package cn.xshi.oauth.web;
import java.util.Iterator;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import cn.xshi.oauth.form.OauthSysModeForm;
import cn.xshi.oauth.model.OauthSysMode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.oauth.service.OauthSysModeService;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Desc 授权中心子系统标记
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/oauthSysMode")
@Api(value = "授权中心子系统标记API",tags = "授权中心子系统标记API",description = "授权中心子系统标记API")
public class OauthSysModeController extends BaseAction {

	@Resource
	private OauthSysModeService oauthSysModeService;

	/**
	 * 查询子系统标记列表并分页
	 * @param oauthSysModeForm
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="查询子系统标记列表并分页", notes="查询子系统标记列表并分页")
	@GetMapping(value="/list")
	@Auth(value = "/oauthSysMode/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<OauthSysMode>> getOauthSysModeListByCondition(OauthSysModeForm oauthSysModeForm,Integer pageNo, Integer pageSize){
		commonHPagerByPageNo(pageNo,pageSize);
		List<OauthSysMode> oauthSysModeList = oauthSysModeService.getOauthSysModeListByCondition(oauthSysModeForm);
		PageInfo<OauthSysMode> page = new PageInfo<OauthSysMode>(oauthSysModeList);
		return outPageBootByPageNo(page,pageNo,pageSize);
	}

	/**
	* 查询单个子系统标记
	* @param id
	*/
	@ApiOperation(value="查询单个子系统标记", notes="查询单个子系统标记")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/oauthSysMode/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<OauthSysMode> getOauthSysModeById(@PathVariable("id")String id){
		OauthSysMode oauthSysMode = oauthSysModeService.getOauthSysModeById(id);
		if(!StringUtil.isEmpty(oauthSysMode.getCreateId())){
			OauthAccountEntity createBy = getAccount(oauthSysMode.getCreateId());
			if(null != createBy){
				oauthSysMode.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(oauthSysMode.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(oauthSysMode.getUpdateId());
			if(null != modifiedBy){
				oauthSysMode.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(oauthSysMode);
	}

	/**
	* 添加
	* @param oauthSysMode 
	*/
	@ApiOperation(value="创建单个子系统标记", notes="创建单个子系统标记")
	@PostMapping(value="/add")
	public BaseResult addOauthSysMode(@RequestBody OauthSysMode oauthSysMode){
		int i = 0;
		if(null != oauthSysMode){
			oauthSysMode.setId(toUUID());
			i=oauthSysModeService.addOauthSysMode(oauthSysMode);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 修改
	* @param oauthSysMode 
	*/
	@ApiOperation(value="编辑单个子系统标记", notes="编辑单个子系统标记")
	@PutMapping(value="/update")
	public BaseResult updateOauthSysMode(@RequestBody OauthSysMode oauthSysMode){
		int i = 0;
		if(null != oauthSysMode){
			i=oauthSysModeService.updateOauthSysMode(oauthSysMode);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 删除
	* @param id
	*/
	@ApiOperation(value="删除子系统标记", notes="删除子系统标记")
	@DeleteMapping(value="/delete")
	public BaseResult delOauthSysMode(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i=oauthSysModeService.delOauthSysMode(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 读取全部数据
	 */
	@ApiOperation(value="查询全部子系统标记", notes="查询全部子系统标记")
	@GetMapping(value="/listAll")
	@Auth(value = "/oauthSysMode/listAll",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<OauthSysMode>> listAll(OauthSysModeForm oauthSysModeForm){
		List<OauthSysMode> oauthSysModeList = oauthSysModeService.getOauthSysModeListByCondition(oauthSysModeForm);
		return outDataStr(oauthSysModeList);
	}

	/**
	 * 导出子系统
	 * @param response
	 * @param id
	 */
	@ApiOperation(value="导出子系统", notes="导出子系统")
	@GetMapping(value = "/export")
	public void exportOauthSysMode(HttpServletResponse response, String id) {
		oauthSysModeService.exportOauthSysMode(response,id);
	}

	/**
	 * 导入资源
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/import")
	@ApiOperation(value="导入子系统", notes="导入子系统")
	public BaseResult importOauthSysMode(HttpServletRequest request, HttpServletResponse response) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;//转型为MultipartHttpRequest：
		MultiValueMap<String, MultipartFile> multipartFileMultiValueMap =  multipartRequest.getMultiFileMap();//获取所有文件
		Iterator<Map.Entry<String, List<MultipartFile>>> iterator = multipartFileMultiValueMap.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry<String, List<MultipartFile>> entry = iterator.next();
			for (MultipartFile multipartFile : entry.getValue()) {
				oauthSysModeService.importOauthSysMode(request,response,multipartFile);
			}
		}
		return BaseResult.success();
	}
}
